
var baseUrl = 'http://localhost:9001';

var pageInfo = {
	page: 1,
	limit: 10,
};

// 预约动作
$('.list').on('click', '.btn-reserve', function(){
	var id = $(this).attr('data-id');
	console.log(id);
	var token = sessionStorage.getItem('token')
	
	$.ajax({
		url: baseUrl + '/fore/reserve',
		data: JSON.stringify({courseId: id}),
		type: 'post',
		dataType: 'JSON',
		contentType: 'application/json; charset=UTF-8',
		beforeSend: function(request) {
			request.setRequestHeader("token", token);
		},
		success(result){
			if (result.code == 200) {
				// 
				new bDialog().open({
					html: '预约成功！',
				});
			} else {
				new bDialog().open({
					title: '警告',
					html: result.msg,
				});
			}
		},
		error(xhr,status,error){
			console.log(xhr)
			if (xhr.status == 401) {
				new bDialog().open({
					title: '警告',
					html: xhr.responseJSON.msg,
				});
			} else {
				new bDialog().open({
					title: '警告',
					html: '请求失败！',
				});
			}
		}
	});
	
});

$('.order li').click(function(){

	$('.order li').removeClass('active');
	$(this).addClass("active");
	
	var order = $(this).attr('data-order');
	
});

loadCourse();

// 列表模板
var template = '\
				<li>\
					<div class="info">\
						<img class="photo" src="">\
					</div>\
					<div class="reserve">\
						<div class="course">瑜伽</div>\
						<div class="coach">小小</div>\
						<div class="time">2024-05-31 18:30:00</div>\
						<a class="btn-reserve">预约</a>\
					</div>\
				</li>';

// 加载课程
function loadCourse(){
	// ajax
	$.ajax({
		url: baseUrl + '/fore/course',
		data: pageInfo,
		dataType: 'json',
		success(result){
			$('.list').empty();
			$.each(result.data.list, function(index, item){
				var dom = $(template);
				dom.find('.photo').attr('src', item.photo);
				dom.find('.course').text(item.name);
				dom.find('.coach').text(item.coach.name);
				dom.find('.time').text(item.time.substring(5, 16));
				dom.find('.btn-reserve').attr('data-id', item.id);
				$('.list').append(dom);
			});
		}
	});
}